package org.sucri.floxs.ext.data;

import org.sucri.floxs.utils.Json_Util;

/**
 * Created by IntelliJ IDEA.
 * User: Wen Yu
 * Date: Jul 15, 2007
 * Time: 7:47:39 PM
 * To change this template use File | Settings | File Templates.
 */
public class JsonReader extends DataReader
{
  public JsonReader(String var) { super(var); init(); }

  private static final String ROOT           = "root";
  private static final String TOTAL_PROPERTY = "totalProperty";
  private static final String ID             = "id";

  public JsonReader(String var, String root, String total, String id)
  {
    super(var);
    setRoot(root);
    setTotalProperty(total);
    setId(id);
  }

  protected void init()
  {
    setRoot("results");
    setTotalProperty("totals");
    setId("id");
  }

  public void setRoot(         String s) { mConfig.put(ROOT, s); }
  public void setTotalProperty(String s) { mConfig.put(TOTAL_PROPERTY, s); }
  public void setId(           String s) { mConfig.put(ID, s); }

  /**
      new Ext.data.JsonReader({
            root: 'Movies',
            totalProperty: 'Total',
            id: 'id'
        }, [
            {name: 'title', mapping: 'title'},
            {name: 'plot', mapping: 'plot'},
            {name: 'release_year', mapping: 'date'},
            {name: 'genre', mapping: 'genre'},
            {name: 'mpaa', mapping: 'mpaa'},
            {name: 'directed_by', mapping: 'directed_by'}
        ])
   * 
   * @return
   */
  @Override
  public String construct()
  {
    String a1 = Json_Util.toJSONString(mConfig);
    String a2 = Json_Util.toJSONString(mRecords, 0);
    return Json_Util.newJs("Ext.data.JsonReader", a1, a2);
  }  
}
